package com.juzi.wopu.web;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.ServletActionContext;

import com.juzi.wopu.domain.CartPlace;
import com.juzi.wopu.domain.CartPlaceResponse;
import com.juzi.wopu.domain.CartSupplier;
import com.juzi.wopu.domain.SysCode;
import com.juzi.wopu.query.CartPlaceQuery;
import com.juzi.wopu.query.PageList;
import com.juzi.wopu.service.ICartPlaceService;
import com.juzi.wopu.service.ICartSupplierService;
import com.juzi.wopu.service.ISysCodeService;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.interceptor.annotations.InputConfig;

public class CartPlaceAction extends CRUDAaction<CartPlace> {
	
	private static final long serialVersionUID = 1L;
	/**
	 * 需要交给spring管理的service
	 */
	private ICartPlaceService cartPlaceService;
	private ISysCodeService sysCodeService;
	private ICartSupplierService cartSupplierService;

	private List<CartPlaceResponse> data=new ArrayList<>();
	private CartPlace cartPlace = new CartPlace();
	private CartPlaceQuery baseQuery = new CartPlaceQuery();
	private PageList pageList;
	
	public void setCartPlaceService(ICartPlaceService cartPlaceService) {
		this.cartPlaceService = cartPlaceService;
	}
	
	public void setSysCodeService(ISysCodeService sysCodeService) {
		this.sysCodeService = sysCodeService;
	}
	
	public void setCartSupplierService(ICartSupplierService cartSupplierService) {
		this.cartSupplierService = cartSupplierService;
	}
	
	public List<CartPlaceResponse> getData() {
		return data;
	}

	public void setData(List<CartPlaceResponse> data) {
		this.data = data;
	}

	@Override
	protected void list() {
		/**
		 * 如果局点不为空，拿到登录进去的局点或者切换的局点
		 * 如果为空就拿到然后赋值给局点这个变量
		 */
		if(companyName!=null){
			ActionContext.getContext().getSession().put("companyName", companyName);
		}
		else {
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
		}
		
		Map<Long, CartSupplier> codeSupplierMap=new HashMap<>();
		cartSupplierService.getCartSupplierMap(codeSupplierMap, null);
		
		Map<String, SysCode> nameCodeMap=new HashMap<>();
		Map<Long, SysCode> map=new HashMap<>();
		sysCodeService.getSysCodeMap(map, nameCodeMap);//得到sName对应SysCode的map
		Long iCompany= nameCodeMap.get(companyName).getiId();
		baseQuery.setiCompany(iCompany);
		this.pageList = cartPlaceService.findByQuery(baseQuery);
		
		if(pageList!=null && pageList.getRows()!=null && pageList.getRows().size()>0){
			for(int i=0;i<pageList.getRows().size();i++){
				CartPlace cartPlace=(CartPlace) pageList.getRows().get(i);
				CartPlaceResponse response=new CartPlaceResponse(cartPlace);
				response.setTypeName(map.get(cartPlace.getiType()).getsName());
				response.setScompany(map.get(cartPlace.getiCompany()).getsName());
				response.setSupplierName(codeSupplierMap.get(cartPlace.getiSupplier()).getsName());
				data.add(response);
			}
		}
	}

	@Override
	public String input() throws Exception {
		/**
		 * 页面添加的时候会在这个方法里面，获得
		 * 供应商和编码表的里面对应的值放入到作用域里面，
		 * 前台页面就可以取对应表里面的值了
		 */
		List<CartSupplier> supplierList= cartSupplierService.getAll();
		List<SysCode> list=sysCodeService.getAll();
		List<SysCode> sysCodeList=new ArrayList<>();
		if(list!=null && list.size()>0){
			for(SysCode sysCode:list){
				if(sysCode.getiParentId()!=null && sysCode.getiParentId()==99L){
					sysCodeList.add(sysCode);
				}
			}
		}
		
		putContext("supplierList", supplierList); 
		putContext("sysCodeList", sysCodeList); 
		return INPUT;
	}

	public void validateSave() {
		if (StringUtils.isBlank(cartPlace.getsName())) {
			addFieldError("name", "名称不能为空");
		}
	}

	@Override
    @InputConfig(methodName="input")
	public String save() throws Exception {
		/**
		 * 如果id为空就保存到最后一页
		 * 设置数据库不为空的字段
		 * 拿到登录进去员工的局点或者切换的局点进行相应的保存
		 */
		if(id==null){
			baseQuery.setCurrentPage(Integer.MAX_VALUE);
			cartPlace.setDtInsert(new Date());
			cartPlace.setsDelay("");
			companyName=(String) ActionContext.getContext().getSession().get("companyName");
			Map<String, SysCode> nameCodeMap=new HashMap<>();
			sysCodeService.getSysCodeMap(null, nameCodeMap);//得到sName对应SysCode的map
			long iCompany=nameCodeMap.get(companyName).getiId();
			cartPlace.setiCompany(iCompany);
			cartPlaceService.save(cartPlace);
		}
		else {
			cartPlaceService.update(cartPlace);
		}
		return RELOAD;
	}

	@Override
	public String delete() throws Exception {
		HttpServletResponse response=ServletActionContext.getResponse();
		response.setContentType("text/json;charset=UTF-8");
		PrintWriter out = response.getWriter();
		try {
			if (id != null) {
				cartPlaceService.delete(id);
				out.print("{\"success\":true,\"msg\":\"删除成功\"}");
			}else{
				out.print("{\"success\":false,\"msg\":\"没有删除成功\"}");
			}
		} catch (Exception e) {
			e.printStackTrace();
			out.print("{\"success\":false,\"msg\":\"异常信息:"+e.getMessage()+"\"}");
		}
		return null;
	}

	@Override
	public void prepareInput() throws Exception {
		if (id != null) {// 最主要做回显
			cartPlace = cartPlaceService.get(id);// 变成栈顶对象
		}
	}

	@Override
	public void prepareSave() throws Exception {
		if (id == null) {
			cartPlace = new CartPlace();
		} else {
			cartPlace = cartPlaceService.get(id);
		}
	}

	@Override
	public CartPlace getModel() {
		return cartPlace;
	}

	public PageList getPageList() {
		return pageList;
	}

	public CartPlaceQuery getBaseQuery() {
		return baseQuery;
	}

	public void setBaseQuery(CartPlaceQuery baseQuery) {
		this.baseQuery = baseQuery;
	}
}
